package com.jcfk.eam.controller.equ;

import cn.hutool.core.util.StrUtil;
import com.jcfk.common.business.log.annotation.RequestLogHeader;
import com.jcfk.common.web.domain.*;
import com.jcfk.eam.domain.dto.main.EamInspectionPlanDTO;
import com.jcfk.eam.domain.dto.main.EamInspectionPlanTeamDTO;
import com.jcfk.eam.domain.dto.main.EamTaskPlanEquDTO;
import com.jcfk.eam.domain.dto.main.OrderBuildRequest;
import com.jcfk.eam.domain.query.main.EamInspectionPlanQuery;
import com.jcfk.eam.enums.AssetKind;
import com.jcfk.eam.enums.TaskKind;
import com.jcfk.eam.service.main.EamInspectionPlanService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 巡检计划
 *
 * @author awei
 * @email 522891237@gqq.com
 * @date 2024-09-24
 */
@Api(tags = {"设备巡检计划管理"})
@RestController
@RequestMapping("equRouteInspectionPlan")
public class EquRouteInspectionPlanController {
    @Autowired
    private EamInspectionPlanService eamInspectionPlanService;

    /**
     * 分页列表
     */
    @ApiOperation("分页查询巡检计划")
    @PostMapping("/queryByPage")
    public RetPage<EamInspectionPlanDTO> queryByPage(@RequestBody ReqPageParam<EamInspectionPlanQuery> requestParam) {
        //设置默认排序
        if(requestParam.getSort().getOrderItem()==null){
            ReqSortParam<EamInspectionPlanQuery> sortParam=new ReqSortParam();
            sortParam.setColumn("lastUpdateTime");
            sortParam.setOrderBy("desc");
            requestParam.setSort(sortParam);
        }
        requestParam.getData().setTaskKind(TaskKind.ROUTE_INSPECTION.getValue());
        DataPage<EamInspectionPlanDTO> page = new DataPage<EamInspectionPlanDTO>((long) requestParam.getCurrent(), (long) requestParam.getSize(), requestParam.getSort().getOrderItem(), EamInspectionPlanDTO.class);
        page = eamInspectionPlanService.queryByPage(page, requestParam.getData());

        return new RetPage<>(page);
    }

    /**
     * 获取设备列表
     *
     * @param query
     * @return
     */
    @ApiImplicitParams({@ApiImplicitParam(paramType = "body", dataType = "EamInspectionPlanQuery", name = "query", value = "", required = true)})
    @ApiOperation(value = "获取设备列表", notes = "获取设备列表", httpMethod = "POST")
    @PostMapping("getDetails")
    public RetResult<List<EamTaskPlanEquDTO>> getDetails(@RequestBody EamInspectionPlanQuery query) {

        List<EamTaskPlanEquDTO> list = eamInspectionPlanService.getDetails(query);

        return RetResult.ok(list);
    }

    /**
     * 获取巡检计划列表
     *
     * @param query
     * @return
     */
    @ApiImplicitParams({@ApiImplicitParam(paramType = "body", dataType = "EamInspectionPlanQuery", name = "query", value = "", required = true)})
    @ApiOperation(value = "获取巡检计划列表", notes = "获取巡检计划列表", httpMethod = "POST")
    @PostMapping("getList")
    public RetResult<List<EamInspectionPlanDTO>> getList(@RequestBody EamInspectionPlanQuery query) {

        List<EamInspectionPlanDTO> list = eamInspectionPlanService.getList(query);

        return RetResult.ok(list);
    }


    /**
     * 巡检计划
     * 根据id 查询详情
     */
    @ApiOperation("查询巡检计划")
    @GetMapping("/get/{id}")
    public RetResult<EamInspectionPlanDTO> get(@PathVariable("id") String id) {
        EamInspectionPlanDTO item = eamInspectionPlanService.get(id);
        return RetResult.ok(item);
    }

    /**
     * 添加/更新管理巡检计划
     */
    @ApiOperation("保存巡检计划")
    @PostMapping("/save")
    @RequestLogHeader(modular = "巡检计划管理", businessType = "保存")
    public RetResult<?> save(@RequestBody @Valid EamInspectionPlanDTO eamInspectionPlan) {
        eamInspectionPlan.setAssetKind(AssetKind.PRODUCTION_EQUIPMENT.getValue());
        eamInspectionPlan.setTaskKind(TaskKind.ROUTE_INSPECTION.getValue());

        // 数据校验
        RetResult ret = eamInspectionPlanService.isValid(eamInspectionPlan);
        if (!ret.getCode().equals(RetResult.SUCCESS)) {
            return ret;
        }

        if(eamInspectionPlan.getInspectionPlanTeams().size()>0){
            String planTeamList="";
            for(EamInspectionPlanTeamDTO planTeam : eamInspectionPlan.getInspectionPlanTeams()){
                planTeamList+=","+planTeam.getTeamName();
            }
            if(StrUtil.isNotEmpty(planTeamList)){
                planTeamList=planTeamList.substring(1);
            }
            eamInspectionPlan.setExecuteTeamName(planTeamList);
        }

        if (StrUtil.isEmpty(eamInspectionPlan.getId())) {
            // 新增
            eamInspectionPlanService.insert(eamInspectionPlan);
        } else {
            // 更新
            eamInspectionPlanService.update(eamInspectionPlan);
        }
        return RetResult.ok();

    }


    /**
     * 批量删除巡检计划
     */
    @ApiOperation("批量删除巡检计划")
    @PostMapping("/delete")
    @RequestLogHeader(modular = "巡检计划管理", businessType = "批量删除")
    public RetResult<?> delete(@RequestBody EamInspectionPlanDTO eamInspectionPlan) {
        return RetResult.ok(eamInspectionPlanService.removeBigBatch(eamInspectionPlan.getIds()));
    }

    @PostMapping("/buildOrder")
    @RequestLogHeader(modular = "设备巡检计划", businessType = "生成巡检工单")
    public RetResult<?> buildOrder(@RequestBody OrderBuildRequest buildRequest) {
        buildRequest.setTaskKind("ri");
        buildRequest.setAssetKind(AssetKind.PRODUCTION_EQUIPMENT.getValue());
        RetResult<String> retResult=eamInspectionPlanService.buildOrder(buildRequest);
        return retResult;
    }


    /**
     * 删除巡检计划
     */
    @ApiOperation("删除巡检计划")
    @PostMapping("/deleteItem/{id}")
    @RequestLogHeader(modular = "巡检计划管理", businessType = "删除")
    public RetResult<?> deleteItem(@PathVariable("id") String id) {
        eamInspectionPlanService.deleteItem(id);
        return RetResult.ok();
    }
}
